<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <ui:composition template="../templates/default3.xhtml">
            <ui:define name="content">
                <h:form id="updateProduct" binding="#{productManagerBean.loadUpdateProduct}">
                    <p:panel id="updateProductPanel" header="Update Product">
                        <p:messages />

                        <table border="0" width="100%">
                            <tr>
                                <td width="170px">Code:</td>
                                <td>
                                    <h:outputText id="code" title="Code of the product."
                                                 value="#{productManagerBean.code}">
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Name:</td>
                                <td>
                                    <p:inputText id="name" label="Product Name" required="true" 
                                                 requiredMessage="Product name cannot be empty."
                                                 title="Name of the product."
                                                 value="#{productManagerBean.pName}">
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Price:</td>
                                <td>
                                    <p:inputText id="price" label="Price" required="true" 
                                                 requiredMessage="Price cannot be empty."
                                                 title="Price of the product."
                                                 value="#{productManagerBean.price}">
                                        <f:convertNumber minFractionDigits="2" />
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Weight:</td>
                                <td>
                                    <p:inputText id="weight" label="Weight" required="true" 
                                                 requiredMessage="Weight cannot be empty."
                                                 title="Weight of the raw material."
                                                 value="#{productManagerBean.weight}">
                                        <f:convertNumber minFractionDigits="2" />
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Capacity per Hour(Bars):</td>
                                <td>
                                    <p:inputText id="capacityPerHour" label="Capacity per Hour" required="true" 
                                                 requiredMessage="Production capacity cannot be empty."
                                                 title="Production capacity for the product."
                                                 value="#{productManagerBean.capacityPerHour}">
                                        <f:convertNumber type="number" />
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Description:</td>
                                <td>
                                    <p:inputTextarea id="description" label="Description" required="true" 
                                                     requiredMessage="Description cannot be empty."
                                                     title="Description of the product."
                                                     style="width:450px;height:75px;" 
                                                     value="#{productManagerBean.pDescription}">
                                    </p:inputTextarea>
                                </td>
                            </tr>
                            <tr>
                                <td width="170px">Status:</td>
                                <td>
                                    <h:selectOneMenu id="selectOneStatus" value="#{productManagerBean.status}"
                                                     title="Status of the product.">
                                        <f:selectItem itemLabel="- Select Status -" itemValue="" />
                                        <f:selectItem itemLabel="Available" itemValue="Available" />
                                        <f:selectItem itemLabel="Discontinued" itemValue="Discontinued" />
                                        <f:selectItem itemLabel="Pending" itemValue="Pending" />
                                    </h:selectOneMenu>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                
                                <td colspan="2" align="center"><hr/>
                                    <p:commandButton id="btnUpdateProduct" value="Update Product"
                                                     ajax="true" title="Update the product."
                                                     update="updateProductPanel"
                                                     disabled="#{productManagerBean.isDiscontinued}"
                                                     actionListener="#{productManagerBean.updateAProduct}"/>
                                    &nbsp;&nbsp;&nbsp;
                                    <p:commandButton id="btnRemoveProduct" value="Remove Product" 
                                                     onclick="confirmation.show()" type="button"
                                                     disabled="#{productManagerBean.isDiscontinued}"
                                                     title="Remove the product." />
                                    &nbsp;&nbsp;&nbsp;
                                    <p:commandButton id="btnBackFromUpdateProduct" value="Back"
                                                     immediate="true" ajax="true"
                                                     title="Go back to the previous page."
                                                     actionListener="#{productManagerBean.back}"/>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel id="materialsPanel" header="Materials Required">
                        <p:messages />
                        <table width="100%" border="0">
                            <tr>
                                <td width="170px">Status:</td>
                                <td>
                                    <h:selectOneMenu id="selectOneRawMaterial" value="#{productManagerBean.rawMaterial.id}"
                                                     title="Status of the product.">
                                        <f:selectItem itemLabel="- Select Raw Material -" itemValue="" />
                                        <f:selectItems value="#{productManagerBean.rawMaterials}" var="rawMaterial" 
                                                       itemLabel="#{rawMaterial.name}" itemValue="#{rawMaterial.id}" />
                                    </h:selectOneMenu>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td>Amount:</td>
                                <td>
                                    <p:inputText id="amount" label="Amount:" required="true" 
                                                 requiredMessage="Amount required cannot be empty."
                                                 title="Amount of the raw material for the product."
                                                 value="#{productManagerBean.materialWeight}">
                                        <f:convertNumber minFractionDigits="2" />
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center"><hr/>
                                    <p:commandButton id="btnAddProductMaterial" value="Add Product Material"
                                                     ajax="true" title="Add materials required for this product."
                                                     update="materialsPanel"
                                                     disabled="#{productManagerBean.isDiscontinued}"
                                                     actionListener="#{productManagerBean.addProductMaterial}"/>
                                <br/><br/>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <p:dataTable id="materials" var="materialRequired" value="#{productManagerBean.materialRequired}" selectionMode="single" 
                                                 selection="#{productManagerBean.selectedMaterialRequired}" update="updateProduct">
                                        <p:column filterBy="#{materialRequired.rawMaterial.name}" headerText="Raw Material Name" filterMatchMode="contains">  
                                            <h:outputText value="#{materialRequired.rawMaterial.name}" />  
                                        </p:column> 
                                        <p:column filterBy="#{materialRequired.weight}" headerText="Weight" filterMatchMode="contains">  
                                            <h:outputText value="#{materialRequired.weight}">
                                                <f:convertNumber minFractionDigits="2" />
                                            </h:outputText>
                                        </p:column> 
                                    </p:dataTable>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center"><hr/>
                                    <p:commandButton id="btnDeleteMaterialRequired" value="Delete Material Required"
                                                     ajax="true" title="Delete material required for this product."
                                                     update="materialsPanel"
                                                     disabled="#{productManagerBean.isDiscontinued}"
                                                     actionListener="#{productManagerBean.deleteProductMaterial}"/>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:confirmDialog  width="400" message="" header="Removing a product" severity="alert" widgetVar="confirmation">
                        <f:facet name="message">
                            <h:outputText value="You are going to remove a product."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="productsPanel" onclick="confirmation.hide()"
                                         actionListener="#{productManagerBean.removeProduct}"/>
                        <p:commandButton value="No" update="productsPanel" onclick="confirmation.hide()" type="button" />
                    </p:confirmDialog>
                </h:form>
            </ui:define>
        </ui:composition>
    </body>
</html>
